package com.zpp.wedding_admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zpp.wedding_admin.entity.Follow;
import com.zpp.wedding_admin.entity.User;
import com.zpp.wedding_admin.service.IFollowService;
import com.zpp.wedding_admin.service.IUserService;
import com.zpp.wedding_admin.utils.LQWUtils;
import com.zpp.wedding_common.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "粉丝管理")
@CrossOrigin
@RestController
@RequestMapping("/weddingadmin/follow")
public class FollowController {

    @Autowired
    private IFollowService followService;

    @Autowired
    private IUserService userService;

    @GetMapping("getFollow/{id}")
    public R getFollow(@PathVariable String id) {
        List<Follow> followList = followService.getFollow(id);
        Integer[] followIds = getIds(followList);
        return R.ok().data("followList", followList).data("followIds", followIds);
    }

    @GetMapping("getFan/{id}")
    public R getFan(@PathVariable String id) {
        List<Follow> fanList = followService.getFan(id);
        Integer[] fanIds = getIds(fanList);
        return R.ok().data("fanList", fanList).data("fanIds", fanIds);
    }

    @GetMapping("unFollow/{oneId}/{twoId}")
    public R unFollow(@PathVariable String oneId, @PathVariable String twoId) {
        LambdaQueryWrapper<Follow> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Follow::getUserOneId, oneId).
                eq(Follow::getUserTwoId, twoId);
        boolean a = userService.followSubtract(oneId);
        boolean c = userService.fanSubtract(twoId);
        boolean b = followService.remove(lqw);
        if (a && b && c) return R.ok();
        return R.error();
    }

    @GetMapping("follow/{oneId}/{twoId}")
    public R follow(@PathVariable String oneId, @PathVariable String twoId) {
        boolean a = userService.followAdd(oneId);
        boolean c = userService.fanAdd(twoId);
        Follow follow = new Follow();
        follow.setUserOneId(Integer.parseInt(oneId));
        follow.setUserTwoId(Integer.parseInt(twoId));
        boolean b = followService.save(follow);
        if (a && b && c) return R.ok();
        return R.error();
    }

    @GetMapping("isTogether/{oneId}/{twoId}")
    public R isTogether(@PathVariable String oneId, @PathVariable String twoId) {
        LambdaQueryWrapper<Follow> lqw = LQWUtils.getLqw(Follow.class);
        lqw.eq(Follow::getUserOneId, oneId).eq(Follow::getUserTwoId, twoId);
        Follow one = followService.getOne(lqw);
        LambdaQueryWrapper<Follow> lqw2 = LQWUtils.getLqw(Follow.class);
        lqw2.eq(Follow::getUserOneId, twoId).eq(Follow::getUserTwoId, oneId);
        Follow two = followService.getOne(lqw2);
        if (one != null && two != null) {
            return R.ok().data("together", 0);//0互粉
        } else if (one != null && two == null) {
            return R.ok().data("together", 1);//1已关注
        } else if (one == null && two != null) {
            return R.ok().data("together", 3);//是粉丝
        } else {
            return R.ok().data("together", 2);//暂未关注
        }
    }

    public Integer[] getIds(List<Follow> list) {
        Map<Integer, Integer> map = new HashMap<>();
        List<Follow> followList = followService.list(null);
        followList.forEach(follow -> map.put(follow.getUserOneId() * 10 + follow.getUserTwoId(), follow.getUserTwoId()));
        Integer[] followIds = new Integer[list.size()];
        for (int i = 0; i < list.size(); i++) {
            Follow follow = list.get(i);
            Integer oneId = follow.getUserOneId();
            Integer twoId = follow.getUserTwoId();
            boolean a = false;
            boolean b = false;
            if (map.containsKey(oneId * 10 + twoId)) {
                if (map.get(oneId * 10 + twoId) == twoId) {
                    a = true;
                }
            }
            if (map.containsKey(twoId * 10 + oneId)) {
                if (map.get(twoId * 10 + oneId) == oneId) {
                    b = true;
                }
            }
            if (a && b) {
                followIds[i] = 0;
            } else if (a && !b) {
                followIds[i] = 1;
            } else if ( !a && b ) {
                followIds[i] = 3;
            } else {
                followIds[i] = 2;
            }
        }
        return followIds;
    }
}
